package com.superdry.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.view.sliding.AbSlidingPlayView;
import com.ab.view.sliding.AbSlidingPlayView.AbOnItemClickListener;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.superdry.activity.BalanceActivity;
import com.superdry.activity.BegineerGuideActivity;
import com.superdry.activity.ChooseCityActivity;
import com.superdry.activity.ChooseClothTypeActivity;
import com.superdry.activity.RegisterActivity;
import com.superdry.activity.SurroundDotActivity;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;

public class Fragment_Dry extends Fragment implements OnClickListener {

	public static final int CHOOSE_CITY = 0;
	AbSlidingPlayView mSlidingPlayView = null;
	public LayoutInflater mInflater;
	private Button surround_dot;
	private Button place_order;
	private TextView select_location;
	private TextView dryer_num;
	private AbHttpUtil mAbHttpUtil = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View settingLayout = inflater.inflate(R.layout.fragment_dry, container, false);
		initViews(settingLayout);
		return settingLayout;
	}

	/**
	 * 初始化组件
	 */
	private void initViews(View settingLayout) {

		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(getActivity());
		mAbHttpUtil.setTimeout(10000);

		mSlidingPlayView = (AbSlidingPlayView) settingLayout.findViewById(R.id.mAbSlidingPlayView);
		surround_dot = (Button) settingLayout.findViewById(R.id.surround_dot);
		place_order = (Button) settingLayout.findViewById(R.id.place_order);
		select_location = (TextView) settingLayout.findViewById(R.id.select_location);
		dryer_num = (TextView) settingLayout.findViewById(R.id.dryer_num);

		mInflater = LayoutInflater.from(getActivity());
		final View mPlayView = mInflater.inflate(R.layout.play_view_item, null);
		ImageView mPlayImage = (ImageView) mPlayView.findViewById(R.id.mPlayImage);
		mPlayImage.setBackgroundResource(R.drawable.pic2);

		final View mPlayView1 = mInflater.inflate(R.layout.play_view_item, null);
		ImageView mPlayImage1 = (ImageView) mPlayView1.findViewById(R.id.mPlayImage);
		mPlayImage1.setBackgroundResource(R.drawable.pic1);

		place_order.setOnClickListener(this);
		surround_dot.setOnClickListener(this);
		select_location.setOnClickListener(this);

		mSlidingPlayView.setNavHorizontalGravity(Gravity.RIGHT);
		mSlidingPlayView.addView(mPlayView);
		mSlidingPlayView.addView(mPlayView1);
		mSlidingPlayView.startPlay();

		mSlidingPlayView.setOnItemClickListener(new AbOnItemClickListener() {

			@Override
			public void onClick(int position) {

				if (position == 0) {
					// 进入新手介绍
					Intent intent = new Intent(getActivity(), BegineerGuideActivity.class);
					startActivity(intent);
				} else if (position == 1) {
					// 进入优惠券介绍
					Intent intent = new Intent(getActivity(), BalanceActivity.class);
					startActivity(intent);
				}

			}
		});
		// 后去使用的人数
		mAbHttpUtil.post(GlobalConst.GET_USER_COUNT, new AbRequestParams(), new AbStringHttpResponseListener() {

			@Override
			public void onStart() {

			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				System.err.println();
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				JSONObject obj = JSONObject.parseObject(content);
				int count = obj.getInteger("i");
				dryer_num.setText(count + "");
			}
		});

	}

	@Override
	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.surround_dot:
			// 周围网点
			intent = new Intent(getActivity(), SurroundDotActivity.class);
			startActivity(intent);
			break;
		case R.id.place_order:
			// 一键下单
			// 判断用户是否登入 没有登入则跳入 注册页面 登入后 直接进入下单界面
			if (SPHelper.getUserInfo(getActivity()) == null) {
				intent = new Intent(getActivity(), RegisterActivity.class);
			} else {
				intent = new Intent(getActivity(), ChooseClothTypeActivity.class);
			}
			startActivity(intent);

			break;
		case R.id.select_location:
			intent = new Intent(getActivity(), ChooseCityActivity.class);
			startActivityForResult(intent, CHOOSE_CITY);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			String city_name = data.getStringExtra("city_name");
			select_location.setText("[" + city_name + "]");
		}

	}
}
